﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
namespace Etalk.Library
{
    public delegate void TraceHandler(string content);
    public class SocketCollection
    {
       
        public event TraceHandler TraceEvent;

        private List<Client> list = new List<Client>();
        public List<Client> List
        {
            get
            {
                return list;
            }
            set
            {
                list = value;
            }
        }
        private Dictionary<string, Client> clientDictionary = new Dictionary<string, Client>();

        public Dictionary<string, Client> ClientDictionary
        {
            get { return clientDictionary; }
        }

        public bool Add(Client obj)
        {
            
            if (clientDictionary.ContainsKey(obj.Name))
                return false;
            else
                clientDictionary.Add(obj.Name, obj);
            list.Add(obj);
            if (TraceEvent != null)
                TraceEvent(string.Format("{0}-{1} Join",obj.Type,obj.Name));
            return true;
        }

        public void Remove(Client obj)
        {
            if (TraceEvent != null)
                TraceEvent(string.Format("{0}-{1} Leave", obj.Type, obj.Name));
            clientDictionary.Remove(obj.Name);
            list.Remove(obj);
        }

        public void Remove(Socket obj)
        {
            Remove(Find(obj));
        }

        public Client FindByKey(string key)
        {
            lock (list)
            {
                foreach (Client item in list)
                {
                    if (item.Name.Equals(key))
                    {
                        return item;
                    }
                }
            }

            return null;
             
        }

        /// <summary>
        /// Find item By ip
        /// </summary>
        /// <param name="ip">ip address</param>
        /// <returns>Client object</returns>
        public Client Find(string ip)
        {
            lock (list)
            {
                foreach (Client item in list)
                {
                    if (item.IP.Equals(ip))
                    {
                        return item;
                       
                    }
                }
            }

            return null;
        }
        /// <summary>
        /// Find item by Socket Object
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public Client Find(Socket obj)
        {
            lock (list)
            {
                foreach (Client item in list)
                {
                    if (item.Sock.Equals(obj))
                    {
                        return item;
                    }
                }
            }
            return null;
        }



    }
}
